package work.jame.blog.configuration.security;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
import work.jame.blog.configuration.filter.JwtAuthenticationTokenFilter;


/**
 * 安全配置类
 *
 * @author Jame!
 * @date 2023/5/9 下午 9:50
 */
@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {
    /**
     * jwt秘钥
     */
    public static String JWT_SECRET_KEY;

    /**
     * 不需要认证可以请求的路径
     */
    private final String[] NO_AUTHORIZE_REQUEST_PATH = new String[]{
            "/user/login",
            "/user/register",
            "/user/createLoginCode",
            "/user/createRegisterCode",
            "/user/createRegisterEmailCode",
            "/user/createLoginEmailCode",
            "/user/createFindPasswordCode",
            "/user/createFindPasswordEmailCode",
            "/user/queryEmailExist",
            "/file/article/img/**",
            "/swagger-ui.html",
            "/swagger-resources/**",
            "/webjars/**",
            "/v2/**",
            "/js/"
    };


    @Autowired
    private JwtAuthenticationTokenFilter jwtAuthenticationTokenFilter;



    @Autowired
    private AccessDeniedHandler accessDeniedHandler;

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.csrf()
                .disable()//关闭cors
                //不通过session获取securityContext
                .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
                .and()
                .authorizeRequests()
                //配置不用认证就可以访问的请求
                .antMatchers(NO_AUTHORIZE_REQUEST_PATH).permitAll()
                //除上面放行的请求外,其他任何请求都需要身份认证
                .anyRequest().authenticated();

        //将自定义的jwt过滤器放在用户密码认证过滤器之前
        http.addFilterBefore(jwtAuthenticationTokenFilter, UsernamePasswordAuthenticationFilter.class);

        //添加自定义异常处理器
        http.exceptionHandling()
//                .authenticationEntryPoint(authenticationEntryPoint)
                .accessDeniedHandler(accessDeniedHandler);

        //允许跨域
        http.cors();
    }

    /**
     * 设置密码加密格式
     *
     * @return PasswordEncoder
     */
    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }


    /**
     * 将认证管理器添加到spring容器当中
     *
     * @return AuthenticationManager
     * @throws Exception
     */
    @Bean
    @Override
    public AuthenticationManager authenticationManager() throws Exception {
        return super.authenticationManager();
    }

    @Value("${jwt.secret-key}")
    public void setJwtSecretKey(String jwtSecretKey){
        JWT_SECRET_KEY=jwtSecretKey;
    }




}
